﻿using System.Linq.Expressions;

using Tszy.Unlimited.Base.Exceptions;
using Tszy.Unlimited.Data.AdoNet.Sql.Components.Expressions.Values;
using Tszy.Unlimited.Data.AdoNet.Sql.Descriptors;

namespace Tszy.Unlimited.Data.AdoNet.Sql.Components.Expressions.Where.Members
{
    internal class MemberAccessExpressionHandler : WhereExpressionHandlerBase
    {
        public override WhereExpressionResult Execute(ExpressionContext context, Expression expression)
        {
            var memberExpression = expression as MemberExpression;

            var result = ValueExpressionHandler.Handler(context, memberExpression);

            if (result.Type == ValueExpressionType.Parameter && result.PropertyType == typeof(bool))
            {
                var memberDescriptor = new WhereMemberExpressionDescriptor
                {
                    ParmeterName = $"{result?.Value}",
                    PropertyType = typeof(bool),
                    ParmeterValue = true
                };

                return new WhereExpressionResult { Status = true, MemberExpressionDescriptor = memberDescriptor };
            }
            else
            {
                throw new MainApplicationException("暂时不支持此表达式");
            }
        }

        public override ExpressionType GetExpressionType()
        {
            return ExpressionType.MemberAccess;
        }
    }
}